package com.ll.beautymall.ui.quality.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ll.beautymall.bean.ObjectProduct;
import com.ll.beautymall.bean.ZeroData;
import com.ll.beautymall.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/26.
 */
public class QualityBiz {
    public void getQualityData(HashMap<String, String> parameters, final OnGetQualityDataLisenter lisenter) {
        HttpUtils.getInstance().getQualityData(parameters, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body();
                        JSONObject object = new JSONObject(json);
                        JSONObject data = object.getJSONObject("data");
                        String next_cursor = data.getString("next_cursor");
                        String previous_cursor = data.getString("previous_cursor");
                        JSONArray products = data.getJSONArray("products");
                        Gson gson = new Gson();
                        List<ObjectProduct> productList = gson.fromJson(products.toString(), new TypeToken<List<ObjectProduct>>() {
                        }.getType());
                        ZeroData quality = new ZeroData(next_cursor, previous_cursor, productList);
                        if (quality != null) {
                            lisenter.onSucced(quality);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }
}
